<template>
    <div class="flex flex-col flex-1 p-2">
        <div class="flex justify-between">
            <div class="flex-1"></div>
            <div>
                <el-button type="primary" @click="openDialog">新增</el-button>
            </div>
        </div>
        <CustomTable class="mt-2" :data="tableData" :total="tableTotal" v-model:page-size="pageSize" @pageSize="getList"
            v-model:currentPage="pageNum" @currentPage="getList">
            <el-table-column type="index" width="60" label="序号" />
            <el-table-column prop="goodsName" label="商品名称" />
            <el-table-column prop="goodsImg" label="图片">
                <template #default="{ row }">
                    <el-image style="width: 100px; height: 100px" :src="row.goodsImg" fit="cover" />
                </template>
            </el-table-column>
            <el-table-column prop="goodsPrice" label="商品价格" />
            <!-- <el-table-column label="商品类型">
                <template #default="{ row }">
                    <div class="flex flex-wrap gap-2">

                        <el-tag v-for="item in row.goodsTypes" type="primary">{{ item.typeName }}</el-tag>
                    </div>
                </template>
            </el-table-column> -->
            <el-table-column label="操作" width="160px">
                <template #default="{ row }">
                    <el-button text type="primary" @click="openEdit(row)">编辑</el-button>
                    <el-button text type="danger" @click="delRow(row)">删除</el-button>
                </template>
            </el-table-column>
        </CustomTable>
    </div>
    <CustomDialog v-model:show="addDialog" :title="(isEdit ? '修改' : '新增') + '商品'" width="1200px" @cancel="cancelDialog"
        @confirm="submitForm">
        <el-form ref="formRef" :model="goodsForm" label-width="auto">
            <el-form-item label="商品名称" prop="goodsName">
                <el-input v-model="goodsForm.goodsName"></el-input>
            </el-form-item>
            <el-form-item label="商品图片" prop="goodsImg">
                <SingleUpload v-model="goodsForm.goodsImg"></SingleUpload>
            </el-form-item>
            <el-form-item label="商品价格" prop="goodsPrice"><el-input
                    v-model="goodsForm.goodsPrice"></el-input></el-form-item>
            <el-form-item label="品类">
                <el-select v-model="goodsForm.categoryId">
                    <el-option v-for="item in categoryList" :value="item.id" :label="item.name"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </CustomDialog>
</template>

<script setup lang="ts">
import CustomTable from '@/components/CustomTable.vue'
import { onMounted, reactive, ref } from 'vue'
import { getGoodsList, addGoods, updateGoods, delGoods, getGoods, getCategoryList } from '@/apis/goods'
import CustomDialog from '@/components/CustomDialog.vue'
import SingleUpload from '@/components/SingleUpload.vue'
import SingleFileUpload from '@/components/SingleFileUpload.vue'
import { ElMessage } from 'element-plus'
import { Delete } from '@element-plus/icons-vue'
import _ from 'lodash'
const tableData = ref([])
const tableTotal = ref(0)
const pageSize = ref(10)
const pageNum = ref(1)
const getList = async () => {
    const res = await getGoodsList({
        pageNum: pageNum.value,
        pageSize: pageSize.value
    })
    tableData.value = res.list
    tableTotal.value = res.total
}

const addDialog = ref()
const openDialog = () => {
    addDialog.value = true
}
const cancelDialog = () => {
    addDialog.value = false
}
const submitForm = async () => {
    console.log(goodsForm.value)

    if (isEdit.value) {
        await updateGoods({
            id: editTarget.value.id,
            goodsName: goodsForm.value.goodsName,
            categoryId: goodsForm.value.categoryId,
            goodsImg: goodsForm.value.goodsImg,
            goodsPrice: goodsForm.value.goodsPrice
        })
        ElMessage.success('编辑成功')
    } else {
        await addGoods({
            goodsName: goodsForm.value.goodsName,
            categoryId: goodsForm.value.categoryId,
            goodsImg: goodsForm.value.goodsImg,
            goodsPrice: goodsForm.value.goodsPrice
        })
        ElMessage.success('添加成功')
    }

    addDialog.value = false
    getList()
}

const editTarget = ref()
const isEdit = ref(false)
const openEdit = async (row) => {
    const res = await getGoodsInfo(row.id)
    console.log(res);
    
    isEdit.value = true
    addDialog.value = true
    const editRow = { ...res }
    Object.assign(goodsForm.value, editRow)
    editTarget.value = row
}
const delRow = async (row) => {
    await delGoods(row.id)
    ElMessage.success('删除成功')
    getList()
}
const goodsForm = ref({
    goodsName: '',
    goodsPrice: '',
    goodsImg: '',
    categoryId: 0
})
const getGoodsInfo = async (id) => {
    return await getGoods(id)
}
const categoryList = ref([])
const getCategorys = async () => {
    const res = await getCategoryList()
    categoryList.value = res
}
onMounted(() => {
    getList()
    getCategorys()
})
</script>

<style scoped></style>
